/**
 * Created by novem on 15/9/25.
 */
    function pi(){
        return  3.1415926535898;
    }
    pow=Math.pow;
    sin=Math.sin;
    cos=Math.cos;
    asin=Math.asin;
    min=Math.min;
    sqrt=Math.sqrt;
window.onload=function(){
    var url = getUrl();
    var loc = getLoc();
    $(".ui-icon-search").on('click',function(){
        var name = $("#search").val();
        var userId = getUserId();
        var prev_name='';
        $.jsonp({
            url: 'http://'+url+'/api/photobook/photobook_list',
            data: {name:name,userId:userId},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                $.each(data.arrays,function(index,item) {
                    earthRadius = 6371;
                    var lat0 = (loc.lat * pi()) / 180;
                    var lng0 = (loc.lng * pi()) / 180;
                    var lat1 = (item.spotsDimensions * pi()) / 180;
                    var lng1 = (item.spotsLongitude * pi()) / 180;
                    calcLongitude = lng1 - lng0;
                    calcLatitude = lat1 - lat0;
                    stepOne = pow(sin(calcLatitude / 2), 2) + cos(lat0) * cos(lat1) * pow(sin(calcLongitude / 2), 2);
                    stepTwo = 2 * asin(min(1, sqrt(stepOne)));
                    calculatedDistance = earthRadius * stepTwo;
                    var unixTimestamp = new Date(item.createTime * 1000);
                    item.createTime = unixTimestamp.toLocaleString();
                    var sex="female";
                    if(item.sex==1)
                        sex="male";
                    if(item.name==name||item.user.nickName==name){
                        if(item.name!=''&&item.name==prev_name){
                            var html='<ul class="order-list">'+
                                '<a href="huace_jingdianhuace_detail.html?photobookId='+item.id+'">'+
                                '<li><div class="order-list-pic">'+
                                '<img src="'+item.photosArray[0]+'" alt=""></div>'+
                                '<div class="order-list-intro"><div class="order-list-intro-text"><p>'+item.name+
                                '</p><p class="color666 fz14"><i class="icons-user"></i>'+item.user.nickName+
                                '<span class="sex sex-'+sex+' no-left"><i class="icons-sex-'+sex+'"></i></span></p>'+
                                '<p class="color666 fz14"><i class="icons-time"></i>'+'发布时间 :'+ item.createTime+'</p></div>'+
                                '<div class="order-list-album-list"><i class="icons-img"></i>'+item.photoNumber+
                                '</div></div></li></a></ul>';
                            $(".ui-container").append(html);
                            item.name='';
                        }else if(prev_name==''||item.name!=prev_name){
                            prev_name=item.name;
                        var html='<div class="ui-list-tips">'+item.name+
                            '景点相关画册如下'+
                            '<div class="ui-list-tips-right"><i class="icons-location"></i><span id="distance"></span>'+calculatedDistance.toFixed(1)+'KM'+
                            '</div></div><ul class="order-list">'+
                            '<a href="huace_jingdianhuace_detail.html?photobookId='+item.id+'">'+
                            '<li><div class="order-list-pic">'+
                            '<img src="'+item.photosArray[0]+'" alt=""></div>'+
                            '<div class="order-list-intro"><div class="order-list-intro-text"><p>'+item.name+
                            '</p><p class="color666 fz14"><i class="icons-user"></i>'+item.user.nickName+
                            '<span class="sex sex-'+sex+' no-left"><i class="icons-sex-'+sex+'"></i></span></p>'+
                            '<p class="color666 fz14"><i class="icons-time"></i>'+'发布时间 :'+ item.createTime+'</p></div>'+
                            '<div class="order-list-album-list"><i class="icons-img"></i>'+item.photoNumber+
                            '</div></div></li></a></ul>'
                        $(".ui-container").append(html);}
                    }else{
                        var html='';
                        $(".ui-container").append(html);
                    }
                });
            },
            error: function (xOptions, textStatus) {
            }
        });
    });
}
function onBackKeyDown() {
    jumpbyback(2,"");
}